package com.bbs.toutiao.controller.client;

import cn.hutool.extra.emoji.EmojiUtil;
import com.bbs.toutiao.exception.BaseException;
import com.bbs.toutiao.exception.ExceptionEnum;
import com.bbs.toutiao.service.client.impl.CommentEditServiceImpl;
import com.bbs.toutiao.utils.Result;
import com.bbs.toutiao.vo.client.AddCommentVo;
import com.bbs.toutiao.vo.comment.CommentPageRequestVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * 描述: 客户端用户评论编辑层
 * 作者： zhangBin
 * 时间： 2023/10/11 17:42
 * 版本： 1.0
 */

@Slf4j
@RestController
@RequestMapping("/api/commentClient")
public class ClientCommentController {

    @Resource
    CommentEditServiceImpl commentEditService;

    /**
     * 客户端用户添加评论
     *
     * @param commentVo
     * @return
     */
    @PostMapping("/insertComment")
    public Result insertComment(@RequestBody AddCommentVo commentVo) {
        log.info("客户端用户添加评论" + commentVo);
        //验证主要数据
        try {
            boolean content = !Objects.equals(commentVo.getContent(), " ");
            boolean userId = commentVo.getArticleId() > 0;
            boolean articleId = commentVo.getUserId() > 0;
            if (!(content && userId && articleId)) {
                return Result.error(400, "评论失败");
            }
        } catch (Exception e) {
            throw new BaseException(ExceptionEnum.NOT_NULL);
        }
        String alias = EmojiUtil.toHtml(commentVo.getContent());
        commentVo.setContent(alias);
        int i = commentEditService.addComment(commentVo);
        if (i > 0) {
            return Result.success("评论成功");
        }
        return Result.error(400, "评论失败");
    }

    /**
     * 查询评论
     *
     * @param pageRequestVo
     * @return
     */
    @PostMapping("/queryCommentClient")
    public Result queryCommentClient(@RequestBody CommentPageRequestVo pageRequestVo) {
        log.info("客户端用户评论" + pageRequestVo);
        if (pageRequestVo.getArticleId() == null || pageRequestVo.getPage() < 1 || pageRequestVo.getSize() < 1) {
            throw new BaseException(ExceptionEnum.BODY_NOT_MATCH);
        }
        pageRequestVo.setPage(pageRequestVo.getPage(), pageRequestVo.getSize());
        return Result.success("获取成功", commentEditService.findByCommentPageClient(pageRequestVo));
    }

    /**
     * 点赞，取消点赞
     *
     * @param comment
     * @return
     */
    @PostMapping("/addCountByCommentId")
    public Result addCountByCommentId(@RequestBody Map<String, Integer> comment) {
        System.out.println(comment);
        if (comment.get("commentId") < 1 && comment.get("userId") < 1) {
            return Result.error(400, "请求数据错误");
        }
        boolean i = commentEditService.addCountByCommentId(comment);
        if (i) {
            return Result.success("成功");
        }
        return Result.error(400, "失败");
    }

    /**
     * 指定用户查询评论
     *
     * @param userId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/commentByUserIdPage/{userId}/{page}/{size}")
    public Result getCommentByUserIdPage(@PathVariable("userId") int userId, @PathVariable("page") int page, @PathVariable("size") int size) {
        if (userId < 0 || page < 1 || size < 1) {
            throw new BaseException(ExceptionEnum.BODY_NOT_MATCH);
        }
        CommentPageRequestVo vo = new CommentPageRequestVo();
        vo.setSize(size);
        vo.setPage(page, size);
        vo.setUserId(userId);
        return Result.success("获取成功", commentEditService.getCommentByUserIdPage(vo));
    }

    //删除指定用户id的评论
    @GetMapping("/deleteCommentByUserId/{id}/{parentId}")
    public Result deleteCommentByUserId(@PathVariable("id") int id, @PathVariable("parentId") int par) {
        log.info("参数为：{}============{}",id,par);
        if (id < 0 && par < 0) {
            return Result.error(404, "参数错误");
        }
        boolean i = commentEditService.deleteComment(id,par);
        if (i) {
            return Result.success("删除成功");
        } else {
            return Result.error(404, "删除失败");
        }
    }
}
